home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Mail / pine3.92 / pico / fileio.c < prev    next >
C/C++ Source or Header  |  1996-03-14  |  3KB  |  136 lines

  1. #if    !defined(lint) && !defined(DOS)
  2. static char rcsid[] = "$Id: fileio.c,v 4.9 1996/03/15 07:41:11 hubert Exp $";
  3. #endif
  4. /*
  5.  * Program:    ASCII file reading routines
  6.  *
  7.  *
  8.  * Michael Seibel
  9.  * Networks and Distributed Computing
  10.  * Computing and Communications
  11.  * University of Washington
  12.  * Administration Builiding, AG-44
  13.  * Seattle, Washington, 98195, USA
  14.  * Internet: mikes@cac.washington.edu
  15.  *
  16.  * Please address all bugs and comments to "pine-bugs@cac.washington.edu"
  17.  *
  18.  *
  19.  * Pine and Pico are registered trademarks of the University of Washington.
  20.  * No commercial use of these trademarks may be made without prior written
  21.  * permission of the University of Washington.
  22.  * 
  23.  * Pine, Pico, and Pilot software and its included text are Copyright
  24.  * 1989-1996 by the University of Washington.
  25.  * 
  26.  * The full text of our legal notices is contained in the file called
  27.  * CPYRIGHT, included with this distribution.
  28.  *
  29.  */
  30. /*
  31.  * The routines in this file read and write ASCII files from the disk. All of
  32.  * the knowledge about files are here. A better message writing scheme should
  33.  * be used.
  34.  */
  35. #include        <stdio.h>
  36. #include    "pico.h"
  37. #include    "estruct.h"
  38. #include        "edef.h"
  39.  
  40.  
  41. FILE    *ffp;                           /* File pointer, all functions. */
  42.  
  43. /*
  44.  * Open a file for reading.
  45.  */
  46. ffropen(fn)
  47.   char    *fn;
  48. {
  49.     int status;
  50.  
  51.     if ((status=fexist(fn, "r", NULL)) == FIOSUC)
  52.       if ((ffp=fopen(fn, "r")) == NULL)
  53.     status = FIOFNF;
  54.  
  55.     return (status);
  56. }
  57.  
  58.  
  59. /*
  60.  * Write a line to the already opened file. The "buf" points to the buffer,
  61.  * and the "nbuf" is its length, less the free newline. Return the status.
  62.  * Check only at the newline.
  63.  */
  64. ffputline(buf, nbuf)
  65.     CELL  buf[];
  66. {
  67.     register int    i;
  68.  
  69.     for (i = 0; i < nbuf; ++i)
  70.        fputc(buf[i].c&0xFF, ffp);
  71.  
  72.     fputc('\n', ffp);
  73.  
  74.     if (ferror(ffp)) {
  75.         emlwrite("Write I/O error");
  76.         return (FIOERR);
  77.     }
  78.  
  79.     return (FIOSUC);
  80. }
  81.  
  82.  
  83.  
  84. /*
  85.  * Read a line from a file, and store the bytes in the supplied buffer. The
  86.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  87.  * at the end of the file that don't have a newline present. Check for I/O
  88.  * errors too. Return status.
  89.  */
  90. ffgetline(buf, nbuf)
  91.   register char   buf[];
  92. {
  93.     register int    c;
  94.     register int    i;
  95.  
  96.     i = 0;
  97.  
  98.     while ((c = fgetc(ffp)) != EOF && c != '\n') {
  99.     /*
  100.      * Don't blat the CR should the newline be CRLF and we're
  101.      * running on a unix system.  NOTE: this takes care of itself
  102.      * under DOS since the non-binary open turns newlines into '\n'.
  103.      */
  104.     if(c == '\r'){
  105.         if((c = fgetc(ffp)) == EOF || c == '\n')
  106.           break;
  107.  
  108.         if (i < nbuf-2)        /* Bare CR. Insert it and go on... */
  109.           buf[i++] = '\r';        /* else, we're up a creek */
  110.     }
  111.  
  112.         if (i >= nbuf-2) {
  113.         buf[nbuf - 2] = c;    /* store last char read */
  114.         buf[nbuf - 1] = 0;    /* and terminate it */
  115.             emlwrite("File has long line");
  116.             return (FIOLNG);
  117.         }
  118.         buf[i++] = c;
  119.     }
  120.  
  121.     if (c == EOF) {
  122.         if (ferror(ffp)) {
  123.             emlwrite("File read error");
  124.             return (FIOERR);
  125.         }
  126.  
  127.         if (i != 0)
  128.       emlwrite("File doesn't end with newline.  Adding one.", NULL);
  129.     else
  130.       return (FIOEOF);
  131.     }
  132.  
  133.     buf[i] = 0;
  134.     return (FIOSUC);
  135. }
  136.